using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class ProductDetail : System.Web.UI.UserControl
{
    processProduct pProduct;
    protected void Page_Load(object sender, EventArgs e)
    {        
        string ProductID = Request.QueryString["ProductID"];
        pProduct = new processProduct();
        Load_ProductDetail(ProductID);
        
    }
    public void Load_ProductDetail(string ProductID)
    {        
        DataTable dtProduct = pProduct.getProductByID(ProductID);
        lblProductName.Text = dtProduct.Rows[0]["ProductName"].ToString();
        imgProductImage.ImageUrl = "Images/" + dtProduct.Rows[0]["ProductImage"];
        lblProductDetail.Text = dtProduct.Rows[0]["ProductDetail"].ToString();
        lblProductPrice.Text = dtProduct.Rows[0]["ProductPrice"].ToString();
        lblProductQuantity.Text = dtProduct.Rows[0]["ProductQuantity"].ToString();
        lblProductWarranty.Text = dtProduct.Rows[0]["ProductWarranty"].ToString();
    }
    
        
    
    
    protected void ibtAddtoCart_Click(object sender, ImageClickEventArgs e)
    {
        string ProductID = Request.QueryString["ProductID"];
        DataTable dtShoppingCart;        
        if (Session["Cart"] == null)
        {
            dtShoppingCart = new DataTable("ShoppingCart");
            dtShoppingCart.Columns.Add("ProductID", typeof(String));
            dtShoppingCart.Columns.Add("ProductName", typeof(String));
            dtShoppingCart.Columns.Add("ProductPrice", typeof(float));
            dtShoppingCart.Columns.Add("Quantity", typeof(int));
            dtShoppingCart.Columns.Add("ProductImage", typeof(String));
            dtShoppingCart.Columns.Add("Total", typeof(float));
            DataRow dr = dtShoppingCart.NewRow();
            dr["ProductID"] = ProductID;
            dr["ProductName"] = lblProductName.Text;
            dr["ProductPrice"] = lblProductPrice.Text;
            dr["ProductImage"] = imgProductImage.ImageUrl;
            dr["Quantity"] = 1;
            dr["Total"] = lblProductPrice.Text;
            dtShoppingCart.Rows.Add(dr);            
        }
        else
        {
            dtShoppingCart = (DataTable)Session["Cart"];
            //Check trung
            bool checkDup = false;
            for (int i = 0; i < dtShoppingCart.Rows.Count;i++ )
            {
                if (dtShoppingCart.Rows[i]["ProductID"].ToString().Equals(ProductID))
                {
                    int Quantity = Int32.Parse(dtShoppingCart.Rows[i]["Quantity"].ToString());
                     Quantity += 1;
                    dtShoppingCart.Rows[i]["Quantity"]=Quantity;
                    checkDup = true;
                    //Response.Write()
                }                
            }
            if (!checkDup)
            {
                DataRow dr = dtShoppingCart.NewRow();
                dr["ProductID"] = ProductID;
                dr["ProductName"] = lblProductName.Text;
                dr["ProductPrice"] = lblProductPrice.Text;
                dr["ProductImage"] = imgProductImage.ImageUrl;
                dr["Quantity"] = 1;
                dr["Total"] = lblProductPrice.Text;
                dtShoppingCart.Rows.Add(dr);
            }
        }        
        
        Session["Cart"] = dtShoppingCart;        
        Response.Redirect("index.aspx?subpara=addtocart");
    }
}
